<?php

namespace App\Kuafu\Shop\Controller\B;

use App\Kuafu\Base\Controller\BaseController;
use App\Kuafu\Base\Enum\BaseCodeEnum;
use App\Kuafu\Base\Validate\BaseValidate;
use App\Kuafu\Export\Model\ExportDownloadModel;
use App\Kuafu\Export\Service\ExportDownloadService;
use App\Kuafu\Order\Enum\OrderMerchantEnum;
use App\Kuafu\Shop\Model\ShopStaffRecordModel;
use App\Kuafu\Shop\Service\ShopStaffRecordService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;


class ShopStaffRecordController extends BaseController
{
    /**
     * @throws ValidationException
     */
    public function list(Request $request): JsonResponse
    {
        $pageNum  = (int)$request->get('pageNum', 1);
        $pageSize = (int)$request->get('pageSize', 10);

        // $this->validate($request, [
        //     'username'        => 'string',
        //     'positionStatus'  => 'int',
        //     'inShop'          => 'int',
        //     'auditStatus'     => 'int',
        //     'auditStartDate'  => 'string',
        //     'auditEndDate'    => 'string',
        //     'updateStartDate' => 'string',
        //     'updateEndDate'   => 'string',
        //     'shopIds'         => 'string',
        //     'dimCode'         => 'string',
        // ]);

        $username        = (string)$request->get('username', '');
        $positionStatus  = (string)$request->get('positionStatus', '');
        $inShop          = (string)$request->get('inShop', '');
        $auditStatus     = (string)$request->get('auditStatus', '');
        $auditStartDate  = (string)$request->get('auditStartDate', '');
        $auditEndDate    = (string)$request->get('auditEndDate', '');
        $updateStartDate = (string)$request->get('updateStartDate', '');
        $updateEndDate   = (string)$request->get('updateEndDate', '');
        $shopIds         = (string)$request->get('shopIds', '');
        $dimCode         = (string)$request->get('dimCode', '');
        $sortField       = (string)$request->get('sortField', '');
        $sortOrder       = (string)$request->get('sortOrder', '');
        $export          = (bool)$request->get('export', 0);

        if ($export){
            $params = [
                'pageSize'        => $pageSize,
                'pageNum'         => $pageNum,
                'username'        => $username,
                'positionStatus'  => $positionStatus,
                'inShop'          => $inShop,
                'auditStatus'     => $auditStatus,
                'auditStartDate'  => $auditStartDate,
                'auditEndDate'    => $auditEndDate,
                'updateStartDate' => $updateStartDate,
                'updateEndDate'   => $updateEndDate,
                'shopIds'         => $shopIds,
                'dimCode'         => $dimCode,
                'sortField'       => $sortField,
                'sortOrder'       => $sortOrder,
            ];
            ExportDownloadService::export(ExportDownloadModel::CLIENT_TYPE_WEB,
                ExportDownloadModel::FILE_TYPE_WEB_SHOP_STAFF_RECORD_LIST, 0,
                OrderMerchantEnum::SHOW_TIME_TAB_DEFINE, strtotime('-1 day'), time(), [], $params
            );
            return $this->success();
        }

        $data = ShopStaffRecordService::list(
            $pageNum,
            $pageSize,
            $username,
            $positionStatus,
            $inShop,
            $auditStatus,
            $auditStartDate,
            $auditEndDate,
            $updateStartDate,
            $updateEndDate,
            $shopIds,
            $dimCode,
            $sortField,
            $sortOrder
        );

        return $this->success($data);
    }


    /**
     * 获得审核详情
     * @param Request $request
     * @param $id
     * @return JsonResponse
     * @throws \Exception
     */
    public function info(Request $request): JsonResponse
    {
        $this->validate($request, [
            'userId' => 'required|integer',
            'shopId' => 'required|integer',
        ]);

        $userId = (int)$request->get('userId');
        $shopId = (int)$request->get('shopId');

        return $this->success(ShopStaffRecordService::info($userId, $shopId));
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Exceptions\BaseException
     */
    public function editHealthCardDate(Request $request)
    {
        $userId              = $request->get('userId');
        $healthCardStartDate = $request->get('healthCardStartDate');
        $optUserId           = request()->user()->id;

        $res = ShopStaffRecordService::editHealthCardDate($userId, $healthCardStartDate, $optUserId);

        return $this->success($res);
    }

    public function submit(Request $request)
    {
        $this->validate($request, [
            'userId'      => 'required|integer',
            'shopId'      => 'required|integer',
            'auditStatus' => 'required|integer|in:' . join(',', array_keys(ShopStaffRecordModel::AUDIT_ACTION_MAP)),
            'remark'      => 'string',
        ], [
            'userId.required'      => "用户id不能为空",
            'shopId.required'      => "门店不能为空",
            'auditStatus.required' => "审核状态不能为空",
        ]);

        $userId      = $request->get('userId');
        $shopId      = $request->get('shopId');
        $auditStatus = $request->get('auditStatus');
        $remark      = $request->get('remark', '');

        ShopStaffRecordService::submit($userId, $shopId, request()->user()->id,$auditStatus, $remark);

        return $this->success();

    }




}
